﻿// -----------------------------------------------------------------------
// <copyright file="Stock_DAL.cs" company="">
// TODO: Update copyright text.
// </copyright>
// -----------------------------------------------------------------------

namespace PMSGSC.DAL
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;

    /// <summary>
    /// TODO: Update summary.
    /// </summary>
    public class Stock_DAL:BaseDAL
    {

        // Lấy tất cả Stock
        public static List<Stock> GetAll(int shipID)
        {
            DB.Refresh(System.Data.Linq.RefreshMode.OverwriteCurrentValues, DB.Stocks);
            return DB.Stocks.Where(n=>n.Ship_ID == shipID).ToList();
        }
        // Lấy Stock theo Id
        public static Stock GetDataById(int id)
        {
            return DB.Stocks.Where(m => m.Stock_ID == id).FirstOrDefault();
        }
        //Thêm một Stock
        public static bool Insert(Stock Stock)
        {
            try
            {
                DB.Stocks.InsertOnSubmit(Stock);
                DB.SubmitChanges();
                return true;
            }
            catch
            {
                return false;
            }

        }
        // Xóa một Stock
        public static bool Delete(Stock Stock)
        {
            try
            {
                DB.Stocks.DeleteOnSubmit(Stock);
                DB.SubmitChanges();
                return true;
            }
            catch
            {
                return false;
            }

        }
        // Cập nhật Stock
        public static bool Update(Stock Stock)
        {
            try
            {
                DB.SubmitChanges();
                return true;
            }
            catch
            {
                return false;
            }

        }

        // Kiểm tra xem Stock Code có tồn tại chưa ?
        public static bool IsExits(Stock Stock)
        {
            Stock objStock = null;
            objStock = DB.Stocks.Where(m => (m.StockCode == Stock.StockCode) && (m.Stock_ID != Stock.Stock_ID) && (m.Ship_ID == Stock.Ship_ID) ).FirstOrDefault();
            return objStock != null ? true : false;
        }
        public static bool IsRelationship(Stock stock)
        {
            LocationInStock location = DB.LocationInStocks.Where(n => n.Stock_ID == stock.Stock_ID).FirstOrDefault();
            if (null != location)
                return false;

            return true;
        }
    }
}
